package com.loanapp.service;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import com.loanapp.domain.LoanApplication;

/**
 * @author Administrator
 */
public class JmsSender {

	private static Log log = LogFactory.getLog(JmsSender.class);

	private JmsTemplate jmsTemplate;

	/**
	 * @return Returns the jmsTemplate.
	 */
	public JmsTemplate getJmsTemplate() {
		return jmsTemplate;
	}

	/**
	 * @param jmsTemplate
	 *            The jmsTemplate to set.
	 */
	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}

	public void sendMessage(final LoanApplication loanApp) {
		jmsTemplate.send(new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {

				log.debug("Creating the message from loan parameters.");

				MapMessage mapMessage = session.createMapMessage();
				mapMessage.setLong("loanAppId", loanApp.getLoanAppId());
				mapMessage.setString("borrowerFirstName", loanApp
						.getFirstName());
				mapMessage.setString("borrowerLastName", loanApp.getLastName());
				mapMessage.setString("ssn", loanApp.getSsn());
				mapMessage.setString("expirationDate", loanApp
						.getExpirationDate());
				mapMessage.setDouble("loanAmount", loanApp.getLoanAmount()
						.doubleValue());
				mapMessage.setInt("creditScore", loanApp.getCreditScore());

				return mapMessage;
			}
		});
	}
}
